package com.mindata.blockmanager.controller.admin;

import cn.hutool.core.bean.BeanUtil;
import com.google.common.base.Preconditions;
import com.mindata.blockmanager.bean.BaseData;
import com.mindata.blockmanager.bean.GridResultDto;
import com.mindata.blockmanager.bean.PermissionParam;
import com.mindata.blockmanager.bean.PermissionVo;
import com.mindata.blockmanager.bean.QueryPage;
import com.mindata.blockmanager.bean.ResultGenerator;
import com.mindata.blockmanager.constant.PermissionType;
import com.mindata.blockmanager.manager.MemberGroupManager;
import com.mindata.blockmanager.manager.PermissionManager;
import com.mindata.blockmanager.model.Permission;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * User: jasperxgwang
 * Date: 2018-8-7 17:15
 */
@Controller
@RequestMapping("/admin/permission")
@Slf4j
public class PermissionAdminController extends BaseController {

    @Resource
    private PermissionManager permissionManager;
    @Resource
    private MemberGroupManager memberGroupManager;

    /**
     * @return
     */
    @RequestMapping("/list")
    @ResponseBody
    public GridResultDto<PermissionVo> list(QueryPage page) {
        GridResultDto memberVos = permissionManager.pageQuery(page);
        return memberVos;
    }

    @RequestMapping("/listPage")
    public ModelAndView listPage() {
        ModelAndView modelAndView = new ModelAndView("permission/permission_list");
        modelAndView.addObject("groupMap", memberGroupManager.getGroupMap());
        return modelAndView;
    }


    @RequestMapping("/add")
    @ResponseBody
    public BaseData add(PermissionParam param) {
        Permission permission = new Permission();
        BeanUtil.copyProperties(param, permission);
        permission.setId(null);
        permission.setCreateTime(new Date());
        permission.setUpdateTime(new Date());
        permissionManager.save(permission);
        return ResultGenerator.genSuccessResult();
    }

    @RequestMapping("/del")
    @ResponseBody
    public BaseData del(long id) {
        permissionManager.del(id);
        return ResultGenerator.genSuccessResult();
    }

    @RequestMapping("/updatePage")
    public ModelAndView updatePage(long id) {
        Preconditions.checkState(id > 0, "error id");
        PermissionVo permissionVo = permissionManager.findById(id);
        Preconditions.checkNotNull(permissionVo, "error id");

        ModelAndView modelAndView = new ModelAndView("permission/permission_update");
        modelAndView.addObject("permissionMap", getPermission());
        modelAndView.addObject("permission", permissionVo);
        modelAndView.addObject("groupMap", memberGroupManager.getGroupMap());
        return modelAndView;
    }

    @RequestMapping("/update")
    @ResponseBody
    public BaseData update(PermissionParam param) {
        Permission permission = new Permission();
        BeanUtil.copyProperties(param, permission);
        permission.setCreateTime(new Date());
        permission.setUpdateTime(new Date());
        permissionManager.updatePermission(permission);
        return ResultGenerator.genSuccessResult();
    }

    @RequestMapping("/addPage")
    public ModelAndView addPage() {
        ModelAndView modelAndView = new ModelAndView("permission/permission_add");
        modelAndView.addObject("permissionMap", getPermission());
        modelAndView.addObject("groupMap", memberGroupManager.getGroupMap());
        return modelAndView;
    }


    private Map<Byte, String> getPermission() {
        Map<Byte, String> permissionMap = new HashMap<>();
        permissionMap.put(PermissionType.ALL.getKey(), PermissionType.ALL.getValue());
        permissionMap.put(PermissionType.ADD.getKey(), PermissionType.ADD.getValue());
        permissionMap.put(PermissionType.UPDATE.getKey(), PermissionType.UPDATE.getValue());
        permissionMap.put(PermissionType.DELETE.getKey(), PermissionType.DELETE.getValue());
        return permissionMap;
    }
}
